package com.oristand.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oristand.pojo.Store;
import com.oristand.service.StoreService;
import com.oristand.util.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Intellij IDEA.
 * 门店信息操作处理
 *
 * @author wtl
 * @version 1.0
 * @date 2021/11/29 14:48
 */
@RestController
@CrossOrigin
public class StoreController {
    private StoreService storeService;

    @Autowired
    public void setStoreService(StoreService storeService) {
        this.storeService = storeService;
    }

    /**
     * 查询所有门店
     * 测试URL：/storeList
     *
     * @param: *@param
     * @return: ResultResponse
     */
    @RequestMapping(path = "/storeList", method = RequestMethod.GET)
    public ResultResponse findStoreList() {
        ResultResponse<List<Store>> resultResponse = new ResultResponse<>();
        List<Store> storeList = storeService.findStoreList();
        if (storeList != null) {
            resultResponse.selectSuccess(storeList);
        } else {
            resultResponse.selectFail(ResultResponse.SELECT_FAIL);
        }
        return resultResponse;
    }

    /**
     * 根据id查询门店
     * 测试URL：/findByStoreId/{storeId}
     *
     * @param: *@param storeId
     * @return: ResultResponse
     */
    @RequestMapping(value = "/findByStoreId/{storeId}", method = RequestMethod.GET)
    public ResultResponse findByStoreId(@PathVariable("storeId") Integer storeId) {
        ResultResponse<Store> resultResponse = new ResultResponse<>();
        Store store = storeService.findByStoreId(storeId);
        if (store != null) {
            resultResponse.selectSuccess(store);
        } else {
            resultResponse.selectFail(ResultResponse.SELECT_FAIL);
        }
        return resultResponse;
    }

    /**
     * 根据Id删除店铺
     * 测试url：/deleteByStoreId/{storeId}
     *
     * @param: *@param storeId
     * @return: ResultResponse
     */
    @RequestMapping(path = "/deleteByStoreId/{storeId}", method = RequestMethod.DELETE)
    public ResultResponse deleteByStoreId(@PathVariable("storeId") Integer storeId) {
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        int row = storeService.deleteByStoreId(storeId);
        if (row > 0) {
            resultResponse.deleteSuccess(row);
        } else {
            resultResponse.deleteFail(ResultResponse.DELETE_FAIL);
        }
        return resultResponse;
    }

    /**
     * 根据Id删除店铺
     * 测试url：/deleteByStoreId/{storeId}
     *
     * @param: *@param storeId
     * @return: ResultResponse
     */
    @RequestMapping(path = "/saveStore", method = RequestMethod.PUT)
    public ResultResponse saveStore(@RequestBody Store store) {
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        store.setStatus(1);
        int row = storeService.saveStore(store);
        if (row > 0) {
            resultResponse.insertSuccess(row);
        } else {
            resultResponse.insertFail(null);
        }
        return resultResponse;
    }


    /**
     * 更新门店信息
     * 测试url：/updateStore
     *
     * @param: *@param store
     * @return: ResultResponse
     */
    @RequestMapping(path = "/updateStore", method = RequestMethod.POST)
    public ResultResponse updateStore(@RequestBody Store store) {
        int row = storeService.updateStore(store);
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        if (row > 0) {
            resultResponse.updateSuccess(row);
        } else {
            resultResponse.updateFail(ResultResponse.UPDATE_FAIL);
        }
        return resultResponse;
    }


    /**
     * 分页查询数据,页码和大小
     * 测试url：/storePage/{pageNum}/{pageSize}
     *
     * @param: *@param pageNum 页码
     * @param: *@param pageSize 每页大小
     * @return: ResultResponse
     */
    @RequestMapping(path = "/storePage/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public ResultResponse storeListByPage(@PathVariable("pageNum") Integer pageNum,
                                          @PathVariable("pageSize") Integer pageSize) {
        ResultResponse<Page<Store>> resultResponse = new ResultResponse<>();
        Page<Store> storeList = storeService.findStoreListByPage(pageNum, pageSize);
        if (storeList != null) {
            resultResponse.selectSuccess(storeList);
            return resultResponse;
        } else {
            resultResponse.selectFail(ResultResponse.SELECT_FAIL);
            return resultResponse;
        }
    }

}
